package com.example.mobile2021606144ywr0617;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button btnMainInsert;
    private RecyclerView rvMainWorker;
    private List<Worker> list = new ArrayList<>();
    MySQLiteOpenHelper openHelper;
    SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        openHelper = new MySQLiteOpenHelper(this, "factory.db", null, 1);
        database = openHelper.getWritableDatabase();
        Cursor cursor = database.query("worker", null, null, null,
                null, null, null);
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String skill = cursor.getString(cursor.getColumnIndex("skill"));
                int money = cursor.getInt(cursor.getColumnIndex("money"));
                list.add(new Worker(id, name, skill, money));
            } while (cursor.moveToNext());
        }
        cursor.close();
        database.close();
        rvMainWorker.setLayoutManager(new LinearLayoutManager(this));
        rvMainWorker.setAdapter(new WorkerViewAdapter(list));
        btnMainInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_insert, null);
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("添加数据")
                        .setView(view)
                        .setPositiveButton("添加", (dialog, which) -> {
                            String name = ((EditText) view.findViewById(R.id.edtTxtDialogName)).getText().toString().trim();
                            String skill = ((EditText) view.findViewById(R.id.edtTxtDialogSkill)).getText().toString().trim();
                            // int类型需要转化Integer.parseInt（）
                            int money = Integer.parseInt(((EditText) view.findViewById(R.id.edtTxtDialogMoney)).getText().toString().trim());
                            ContentValues values = new ContentValues();
                            values.put("name", name);
                            values.put("skill", skill);
                            values.put("money", money);
                            database = openHelper.getWritableDatabase();
                            database.insert("worker", null, values);
                            list.clear();
                            Cursor cursor = database.query("worker", null, null, null,
                                    null, null, null);
                            if (cursor.moveToFirst()) {
                                do {
                                    int id = cursor.getInt(cursor.getColumnIndex("_id"));
                                    name = cursor.getString(cursor.getColumnIndex("name"));
                                    skill = cursor.getString(cursor.getColumnIndex("skill"));
                                    money = cursor.getInt(cursor.getColumnIndex("money"));
                                    list.add(new Worker(id, name, skill, money));
                                } while (cursor.moveToNext());
                            }
                            rvMainWorker.getAdapter().notifyDataSetChanged();
                            cursor.close();
                            database.close();
                        })
                        .setNegativeButton("取消", (dialog, which) -> {
                        })
                        .create()
                        .show();
            }
        });
    }

    private void initView() {
        btnMainInsert = (Button) findViewById(R.id.btnMainInsert);
        rvMainWorker = (RecyclerView) findViewById(R.id.rvMainWorker);
    }
}